var $file={
	locale:{ja:{open:"Open",save:"Save"}},
	cosSvc:Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream),
	fisSvc:Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream),
	convSvc: Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
	localSvc: Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile),
	cisSvc: Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream),
	sisSvc:Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream),
	fosSvc:Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream),
	svc:function(id){
		const CC = Components.Constructor;
		const Cc = Components.classes;
		const Ci = Components.interfaces;
		
		switch (id){
			case 'cos':
				return Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
				break;
			case 'local':
				return Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
				break;
			case 'fp':
				return Cc['@mozilla.org/filepicker;1'].createInstance(Ci.nsIFilePicker)
				break;
		}
	
	},
	init:function(path,file){
	//	this.log(path+file);
		var obj = $file.svc("local");
		if(path){obj.initWithPath(path);}
		if(file){obj.append(file);}
		return obj;
	},
	dir:function (key){
		return Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(key, Components.interfaces.nsIFile);
	},
	
	write:function(file,content){
		var charset = "UTF-8";

		if (file.exists()){file.remove(true);}
		file.create(file.NORMAL_FILE_TYPE, 0666);
			
		var fosObj = this.fosSvc;
		fosObj.init(file, 0x02, 0664, 0);
		
		var cosObj = this.svc("cos");
		cosObj.init(fosObj, charset, 0, 0x0000);
		cosObj.writeString(content);
		cosObj.close();
	},
	
	picker:function(mode,filter){
	
		const fp= this.svc("fp");
		const nsIFilePicker = Components.interfaces.nsIFilePicker;
		
		
		switch (mode){
			case "save":
				var objMode=nsIFilePicker.modeSave;
				break;
			case "folder":
				var objMode=nsIFilePicker.modeGetFolder;
				break;
			case "multiple":
				var objMode=nsIFilePicker.modeOpenMultiple;
				break;
			case "open":
			default:
				var objMode=nsIFilePicker.modeOpen;
				var lblMode=this.locale.ja.open;
				break;
		}
	//	alert(filter)
		
		for (var key in filter){
			fp.appendFilter(key,filter[key])
		}
	//	fp.appendFilters(nsIFilePicker.filterXML)
		
		//fp.appendFilter("CSV 形式のファイル","*.csv")
		
		fp.init(window,lblMode,objMode);
		var result = fp.show();

		switch (result){
			case nsIFilePicker.returnOK:
				return fp.file;
				break;
			case nsIFilePicker.returnReplace:
				return fp.file;
				break;
			default:
			case nsIFilePicker.returnCancel:
				break;
		}
		

	},

	read:function(file){
		var fisObj = this.fisSvc;
		var charset =  "UTF-8";
		var cisObj = this.cisSvc;
		
		fisObj.init(file, -1, 0, 0);
		cisObj.init(fisObj, charset, 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
		var str = {};
		var data="";
		while (cisObj.readString(4096, str) != 0) {
			data += str.value;
			this.log(str.value)
		}
		cisObj.close();
		return data;
	},

	log: function (str){
		Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService).logStringMessage(str);
	},
	

};